from infirmary_admin_src.infirmary_common.infirmary_controller import IBaseController
from infirmary_admin_src.infirmary_common.infirmary_controller.rest_controller_with_depends import \
    RestControllerWithDepends
from fastapi import Request, Depends, Path, Body

from infirmary_admin_src.infirmary_smart_admin.infirmary_biz.infirmary.businesses.schemas import SchemaBase
from infirmary_admin_src.infirmary_smart_admin.infirmary_biz.system.login.services import LogicBusinesses



class XParam(SchemaBase):
    '''
    账号密码登录参数
    '''
    parent_id: int = Body(0, alias='parentId', title="parent_id", description='父类的ID')
    # 修改的其他信息
    api_perms: str = Body(None, alias='apiPerms', title="visit_openid", description='API接口的权限标记')
    cache_flag: int = Body(default=0, alias='cacheFlag', title="cache_flag", description='缓存标记')
    component: str = Body(None, title="component", description='component组件信息')
    create_time: str = Body('now()', alias='createTime', title="create_time", description='创建时间')
    create_user_id: int = Body(0, alias='createUserId', title="create_user_id", description='创建用户ID')
    deleted_flag: int = Body(0, alias='deletedFlag', title="deleted_flag", description='是否删除标记')
    frame_flag: int = Body(0, alias='frameFlag', title="frame_flag", description='是否是外链')
    disabled_flag: int = Body(0, alias='disabledFlag', title="disabled_flag", description='是否禁用')
    frame_url: str = Body(None, alias='frameUrl', title="frame_url", description='外链地址URL')
    icon: str = Body(None, alias='icon', title="icon", description='菜单图标')
    menu_name: str = Body('', alias='menuName', title="menu_name", description='菜单名称')
    menu_type: int = Body(default=0, alias='menuType', title="menu_type", description='菜单类型')
    path: str = Body(None, alias='path', title="path", description='菜单路径')
    perms_type: int = Body(None, alias='permsType', title="perms_type", description='权限类型')
    sort: int = Body(0, alias='sort', title="sort", description='排序')
    update_time: str = Body('now()', alias='updateTime', title="update_time", description='更细时间')
    update_user_id: int = Body(None, alias='updateUserId', title="update_user_id", description='更细用户ID')
    visible_flag: int = Body(1, alias='visibleFlag', title="visible_flag", description='是否显示')
    web_perms: str = Body(None, alias='webPerms', title="web_perms", description='web权限标记')
    is_trusted: bool = Body(True, alias='isTrusted', title="is_trusted", description='是否显示')


class IController(IBaseController):
    '''
    定制相关约束检测函数逻辑步序
    '''

    def __init__(self, *, request: Request,
                 schema_param: XParam,
                 ):
        super().__init__(request=request)
        # 入参参数
        # 入参参数
        self.schema_param = schema_param


@RestControllerWithDepends()
class SystemMenuAddController(IController):

    def business_login(self):
        pass
        # 自增ID获取
        menu_id_max_id = {
            'menu_id': LogicBusinesses.get_sys_menu_id_max_id() + 1,
        }
        # 执行添加菜单
        if self.schema_param.menu_type != 3:
            # 去除类型
            self.schema_param.perms_type = None
        # 执行菜单添加
        result = LogicBusinesses.add_sys_menu(
            **{**self.schema_param.dict(exclude={'menu_id', 'api_permslist', 'is_trusted'}),
               **menu_id_max_id})
        if result:
            self.response_msg = '添加成功'
        else:
            self.response_ok = False
            self.response_msg = '添加失败'
            return None

        return None
